package cn.tedu.boyou.mgrsite.listener;

import cn.tedu.boyou.acticle.domain.StrategyDetail;
import cn.tedu.boyou.acticle.service.IStrategyDetailService;
import cn.tedu.boyou.acticle.vo.StrategyStatisVO;
import cn.tedu.boyou.cache.prefix.StrategyStatisKeyPrefix;
import cn.tedu.boyou.cache.service.IRedisService;
import cn.tedu.boyou.cache.service.IStrategyStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  redis 数据初始化监听器
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IRedisService redisService;

    //当spring容器启动后，马上执行的方法
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //将mysql中的数据，同步到redis缓存数据库中
        List<StrategyDetail> list = strategyDetailService.listAll();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            if(redisService.exists(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,detail.getId()+"")){
                continue;
            }
            vo = new StrategyStatisVO();
            vo.setIsabroad(detail.isIsabroad());
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());
            vo.setStrategyId(detail.getId());
            vo.setThumbsupnum(detail.getThumbsupnum());
            vo.setSharenum(detail.getSharenum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getFavornum());
            vo.setViewnum(detail.getViewnum());
            strategyStatisRedisService.setStatisVo(vo);

        }

        //海外 + 国内
        for (StrategyDetail detail : list) {
            //实际开发操作：
            //1:方案:进行数据初始化，先判断zset中value值是否存在，如果存在跳过，如果不存在，则初始化

            //判断value值是否存在zset中
            //
            if(!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,""
                    ,StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix()+":"+detail.getId())){
                //收藏数+点赞数
                int score = detail.getFavornum() + detail.getThumbsupnum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,detail.getId(),score);
            }


        }

        //热门攻略的初始化
        for (StrategyDetail detail : list) {
            if(!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,""
                    ,StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix()+":"+detail.getId())){
                //收藏数+点赞数
                int score = detail.getViewnum() + detail.getReplynum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,detail.getId(),score);
            }
        }
    }
}



















